package cn.dankal.flyaudio.home

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.DividerItemDecoration
import android.view.View
import cn.dankal.basiclib.base.BaseRvFragmentImp
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewAdapter
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewPresenter
import cn.dankal.basiclib.base.recyclerview.OnRvItemClickListener
import cn.dankal.basiclib.protocol.MainProtocol
import cn.dankal.business.pojo.ActivityBean
import cn.dankal.business.Constants
import cn.dankal.business.Constants.*
import cn.dankal.flyaudio.R
import cn.dankal.flyaudio.activity.search.SearchActivity
import com.alibaba.android.arouter.launcher.ARouter
import com.yzq.zxinglibrary.android.CaptureActivity
import kotlinx.android.synthetic.main.fragement_activity.*

class ActivityFragment : BaseRvFragmentImp<ActivityBean>() {
    override fun getLayoutId(): Int {
        return R.layout.fragement_activity
    }
    companion object {

        fun newInstance():ActivityFragment {
            val fragment = ActivityFragment()
            var bundle = Bundle()
            bundle.putInt(TYPE,TYPE_ALL)
            fragment.arguments=bundle
            return fragment
        }
    }

    private var isCreated = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isCreated=true
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)

        if (!isCreated) {
            return
        }

        if (isVisibleToUser) {
            obtainData()
        }
    }

    override fun initComponents() {
        super.initComponents()
        setTranslucent(activity,ll_scan_title)
//        recyclerView.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
        tv_search.setOnClickListener {
            startActivity(Intent(context, SearchActivity::class.java))

        }
        iv_scan.setOnClickListener {
            activity?.startActivityForResult(Intent(context, CaptureActivity::class.java), Constants.SCAN)
        }
    }
    override fun getPresenter(): BaseRecyclerViewPresenter<ActivityBean>? {
        return ActivityPrsenter("")
    }

    override fun getAdapter(): BaseRecyclerViewAdapter<ActivityBean>? {
        var activityAdapter = ActivityAdapter2()
        activityAdapter.setOnRvItemClickListener(object : OnRvItemClickListener<ActivityBean>() {
            override fun onItemClick(v: View?, position: Int, data: ActivityBean) {
                ARouter.getInstance().build(MainProtocol.ACTIVITY_DETAIL)
                        .withString(UUID,data.uuid)
                        .navigation()
            }
        })
        return activityAdapter
    }
}
